//
// 页面跳转前操作
// type 1 切换底部菜单
// type 2 销毁上个页面
//
export function nextRouter(url, type) {
	console.log('to:', url, type);
	if (type == '1') {
		uni.switchTab({
			url: '/pages' + url,
		});
	} else if (type == '2') {
		uni.redirectTo({
			url: '/pages' + url,
		});
	} else {
		uni.navigateTo({
			url: '/pages' + url,
		});
	}
}

/* 函数防抖*/
export function debounce(fn, interval) {
	var timer;
	// 间隔时间，如果interval不传，则默认1000ms
	var gapTime = interval || 1000;
	return function () {
		clearTimeout(timer);
		var context = this;
		// 保存此处的arguments，因为setTimeout是全局的，arguments不是防抖函数需要的。
		var args = arguments;
		timer = setTimeout(function () {
			fn.call(context, args);
		}, gapTime);
	};
}

//* 号处理手机号
export function hidePhone(str) {
	if (null != str && str != undefined) {
		let pat = /(\d{3})\d*(\d{4})/;
		return str.replace(pat, '$1****$2');
	} else {
		return ' ';
	}
}

// 经纬度转换成三角函数中度分表形式。
function rad(d) {
	return (d * Math.PI) / 180.0;
}

// 根据经纬度计算距离，参数分别为第一点的纬度，经度；第二点的纬度，经度
export function getDistance(lat1, lng1, lat2, lng2) {
	let radLat1 = rad(lat1);
	let radLat2 = rad(lat2);
	let a = radLat1 - radLat2;
	let b = rad(lng1) - rad(lng2);
	let s =
    2 *
    Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2) +
          Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2))
    );
	// EARTH_RADIUS;
	s = s * 6378.137; 
	// 输出为公里
	s = Math.round(s * 10000) / 10000; 

	let distance = s;
	// eslint-disable-next-line id-match
	let distance_str = '';

	if (parseInt(distance) >= 1) {
		distance_str = distance.toFixed(1) * 1000;
	} else {
		distance_str = distance * 1000;
	}

	// s=s.toFixed(4);

	// console.info('lyj 距离是:', distance_str, lat1, lng1, lat2, lng2);
	return distance_str;
}

// 根据经纬度计算得出的距离,判断地图的缩放比例  distance距离 单位m
export function getScale(distance) {
	// 公里
	let kilometer = distance / 1000; 
	if (kilometer >= 1000) {
		return 3;
	} else if (500 <= kilometer) {
		return 5;
	} else if (200 <= kilometer) {
		return 6;
	} else if (100 <= kilometer) {
		return 7;
	} else if (50 <= kilometer) {
		return 8;
	} else if (20 <= kilometer) {
		return 9;
	} else if (10 <= kilometer) {
		return 10;
	} else if (5 <= kilometer) {
		return 11;
	} else if (3 <= kilometer) {
		return 12;
	} else if (2 <= kilometer) {
		return 13;
	} else if (1 <= kilometer) {
		return 14;
	} else if (0.5 <= kilometer) {
		return 15;
	} else {
		return 16;
	}
}

// 自定义导航栏时 内容上边距
export function navHeight() {
	let navInfo =  uni.getMenuButtonBoundingClientRect();
	// #ifdef H5
	navInfo = { top: 24 };
	// #endif
	
	return navInfo;
	// if (state == 1) {
	// 	// navInfo.height=>胶囊的高度 navInfo.top胶囊上面的高度 uni.upx2px(height)=>胶囊下面元素的高度,height传入为rpx值 方法返回是px单位
	// 	return navInfo.height + navInfo.top + uni.upx2px(height);
	// } else {
	// 	return navInfo;
	// }
}

// 计算梯龄
export function getDiffYear(dd) {
	if (!dd) {
		return '-';
	}

	let date = new Date(dd.replace(/-/g, '/'));
	let now = new Date();
	let diff = now.getTime() - date.getTime();
	if (diff < 0) return '-';
	date = null;
	now = null;
	return (diff / (1000 * 60 * 60 * 24) / 365).toFixed(1);
}

// 获取过去n天的时间段 getLastDates(n) => yyyy-MM-dd~yyyy-MM-dd
export function getLastDates(n, today) {
	const addZero = (num) => {
		return num >= 10 ? num : '0' + num;
	};
	let now = new Date();
	let lastTime;
	if (n == 0) {
		lastTime = now.getTime();
	} else {
		now.setTime(now.getTime() - (today ? 0 : 60 * 60 * 24 * 1000));
		lastTime =
      now.getTime() - 60 * 60 * 24 * 1000 * ((n || 0) - (today ? 0 : 1));
	}
	let lastDate = new Date(lastTime);

	return {
		startTime:
      lastDate.getFullYear() +
      '-' +
      addZero(lastDate.getMonth() + 1) +
      '-' +
      addZero(lastDate.getDate()),
		endTime:
      now.getFullYear() +
      '-' +
      addZero(now.getMonth() + 1) +
      '-' +
      addZero(now.getDate()),
	};
}

// 数组去重
export function delRepeat(list, key) {
	for (let i = 0; i < list.length; i++) {
		for (let j = i + 1; j < list.length; j++) {
			if (key) {
				if (list[i][key] == list[j][key]) {
					list.splice(j, 1);
					j--;
				}
			} else {
				if (list[i] == list[j]) {
					list.splice(j, 1);
					j--;
				}
			}
		}
	}
	return list;
}

// 节流
export function throttle(func, delay) {
	let prev = Date.now();
	return function () {
		let context = this;
		let args = arguments;
		let now = Date.now();
		if (now - prev >= delay) {
			func.apply(context, args);
			prev = Date.now();
		}
	};
}
